package com.liuxing.javaSpringBoot.modules.test.dao;

import com.liuxing.javaSpringBoot.modules.common.entity.SearchBean;
import com.liuxing.javaSpringBoot.modules.test.entity.City;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * @File CityDao.java
 * @Auth liuxing
 * @Date 2021/8/19 10:35
 * @Email liuxing997@foxmail.com
 */
@Mapper
@Repository
public interface CityDao {

    //添加城市信息
    @Insert("insert into test_city (city_name, local_city_name, country_id, date_created) " +
            "values (#{cityName}, #{localCityName}, #{countryId}, #{dateCreated})")
    @Options(useGeneratedKeys = true, keyColumn = "city_id", keyProperty = "cityId")
    void insertCity(City city);

    //根据城市ID修改城市信息
    @Update("update test_city set city_name = #{cityName} , local_city_name = #{localCityName} " +
            "where city_id = #{cityId}")
    void updateCity(City city);

    //根据城市ID删除城市
    @Delete("delete from test_city where city_id = #{cityId}")
    void deleteCityByCityId(int cityId);

    //根据城市ID查询城市
    @Select("select * from test_city where city_id=#{cityId}")
    City getCityByCityId(int cityId);

    //根据国家ID查询所有城市信息
    @Select("select * from test_city where country_id=#{countryId}")
    List<City> getCitiesByCountryId(int countryId);

    //分页查询
    @Select("<script>"
            + "select * from test_city "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (city_name like '%${keyword}%' or "
            + " local_city_name like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by city_id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<City> getCitiesBySearchBean(SearchBean searchBean);

    //插入一个City list
    @Insert("<script>"
            + "insert into test_city (city_name, local_city_name, country_id, district, "
            + "population, date_modified, date_created) values "
            + "<foreach collection='cities' item='city' index='index' separator=','>"
            + "(#{city.cityName}, #{city.localCityName}, #{city.countryId}, "
            + "#{city.district}, #{city.population}, #{city.dateModified}, #{city.dateCreated})"
            + "</foreach>"
            + "</script>")
    @Options(useGeneratedKeys = true,keyColumn = "city_id",keyProperty = "cityId")
     void insertCities(@Param("cities") List<City> cities);
}
